//
//  PaymentConfirm.m
//  DaoZiJinFu
//
//  Created by woker on 16/6/29.
//  Copyright © 2016年 iwoker. All rights reserved.
//

#import "PaymentConfirm.h"

@implementation PaymentConfirm

+ (instancetype)showView:(UIView *)view withMobile:(NSString *)mobile {
    PaymentConfirm *pay = [[[NSBundle mainBundle] loadNibNamed:@"PaymentConfirm" owner:nil options:nil] lastObject];
    pay.msgAlert.text = [NSString stringWithFormat:@"本次交易需要短信确认，校验码已发送至您手机%@",mobile];
    pay.frame = SCREEN_FRAME;
    [view addSubview:pay];
    
//    [pay mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(view.mas_top);
//        make.leading.equalTo(view.mas_leading);
//        make.bottom.equalTo(view.mas_bottom);
//        make.trailing.equalTo(view.mas_trailing);
//    }];
    
    [pay readjustView];
    
    return pay;
}

- (IBAction)closeView:(id)sender {
    [_timer invalidate];
    _timer = nil;
    [self removeFromSuperview];
}

- (IBAction)comfirmClick:(id)sender {
//    NSLog(@"%@",_msgCodeText.text);
    [self.delegate confirmRecharge:_msgCodeText.text];
}

- (IBAction)cancelBtnClick:(id)sender {
    [_timer invalidate];
    _timer = nil;
    [self removeFromSuperview];
}

- (void)readjustView {
    _alert.layer.cornerRadius = 10;
    _alert.layer.masksToBounds = YES;
//    _comfirmBtn.layer.cornerRadius = BTN_RADIUS;
//    _comfirmBtn.layer.masksToBounds = YES;
    _msgCodeText.layer.borderColor = BLUE_COLOR.CGColor;
    _msgCodeText.layer.borderWidth = 1;
//    _cancelBtn.layer.cornerRadius = BTN_RADIUS;
//    _cancelBtn.layer.masksToBounds = YES;
//    _cancelBtn.layer.borderColor = ORANGECOLOR.CGColor;
//    _cancelBtn.layer.borderWidth = 1;
    
    [_msgCodeText becomeFirstResponder];
    
    [_receiveCodeBtn addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(receiveCodeClick)]];
    _receiveCodeBtn.userInteractionEnabled = NO;
    _timerCount = timeLine;
//    _receiveCodeBtn.backgroundColor = UNUSEABLECOLOR;
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeDown) userInfo:nil repeats:YES];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    CGRect rect = self.frame;
    rect.origin.y = rect.origin.y - 100;
    [UIView animateWithDuration:0.3 animations:^{
        self.frame = rect;
    }];
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    CGRect rect = self.frame;
    rect.origin.y = rect.origin.y + 100;
    [UIView animateWithDuration:0.3 animations:^{
       self.frame = rect;
    }];
}

- (void)timeDown {
    if (_timerCount == 0) {
        [_timer invalidate];
        _timer = nil;
        _receiveCodeBtn.userInteractionEnabled = YES;
        _receiveCodeBtn.backgroundColor = BLUE_COLOR;
        _receiveCodeBtn.text = @"重新获取";
        _timerCount = timeLine;
        return;
    }
    _receiveCodeBtn.text = [NSString stringWithFormat:@"%lds",_timerCount--];
}

- (void)receiveCodeClick {
    _receiveCodeBtn.userInteractionEnabled = NO;
//    _receiveCodeBtn.backgroundColor = UNUSEABLECOLOR;
    _receiveCodeBtn.text = [NSString stringWithFormat:@"%lds",_timerCount--];
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeDown) userInfo:nil repeats:YES];
    [self.delegate sendCode];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    _msgCodeText.delegate = self;
     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeKeybord)];
    [self addGestureRecognizer:tap];
}

- (void)removeKeybord {
    [self endEditing:YES];
}
@end
